package com.tsingtree.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.modular.entity.*;
import com.tsingtree.modular.mapper.NanSellerDeviceMapper;
import com.tsingtree.modular.model.params.NanSellerDeviceParam;
import com.tsingtree.modular.model.result.NanSellerDeviceResult;
import com.tsingtree.modular.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.*;

import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.stream.Collectors;


/**
 * <p>
 * 运营组-设备 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2023-08-29
 */
@Service
public class NanSellerDeviceServiceImpl extends ServiceImpl<NanSellerDeviceMapper, NanSellerDevice> implements NanSellerDeviceService {

    @Autowired
    private NanUserDeviceService nanUserDeviceService;
    @Autowired
    private NanDeviceCateService nanDeviceCateService;
    @Autowired
    private NanSellerService nanSellerService;
    @Autowired
    private NanSellerTagService nanSellerTagService;

    @Override
    public void add(NanSellerDeviceParam param){
        NanSellerDevice entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanSellerDeviceParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanSellerDeviceParam param){
        NanSellerDevice oldEntity = getOldEntity(param);
        NanSellerDevice newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanSellerDeviceResult findBySpec(NanSellerDeviceParam param){
        return null;
    }

    @Override
    public List<NanSellerDeviceResult> findListBySpec(NanSellerDeviceParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanSellerDeviceParam param){
        Page pageContext = getPageContext();
        IPage<NanSellerDeviceResult> page = this.baseMapper.customPageList(pageContext, param);
        Set<String> codes = new HashSet<>();
        Set<Integer> sellers = new HashSet<>();
        for (NanSellerDeviceResult record : page.getRecords()) {
            codes.add(record.getCode());
            sellers.add(record.getSellerId());
        }
        Map<String, Integer> countMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(codes)){
            List<Map<String, Object>> code = nanUserDeviceService.listMaps(new QueryWrapper<NanUserDevice>().select("code, count(1) as count").groupBy("code"));
            for (Map<String, Object> stringObjectMap : code) {
                String c = stringObjectMap.get("code").toString();
                int count = Integer.parseInt(stringObjectMap.get("count").toString());
                countMap.put(c, count);
            }
        }
        Map<Integer, String> sellerMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(sellers)){
            sellerMap = nanSellerService.list(new QueryWrapper<NanSeller>().in("id", sellers)).stream().
                    collect(Collectors.toMap(NanSeller::getId, NanSeller::getName));
        }
        Map<Integer, String> tagMap = nanSellerTagService.list().stream().collect(Collectors.toMap(NanSellerTag::getId, NanSellerTag::getName));
        Map<Integer, String> cateMap = nanDeviceCateService.list().stream().collect(Collectors.toMap(NanDeviceCate::getId, NanDeviceCate::getName));
        for (NanSellerDeviceResult record : page.getRecords()) {
            record.setNum(countMap.getOrDefault(record.getCode(), 0));
            record.setCate(cateMap.get(record.getCateId()));
            record.setSellerName(sellerMap.get(record.getSellerId()));
            String tag = record.getTag();
            if(StringUtils.hasText(tag)){
                String[] tags = tag.split(",");
                StringBuilder names = new StringBuilder();
                for(String tagStr : tags){
                    int key = Integer.parseInt(tagStr.substring(1, tagStr.length() - 1));
                    String name = tagMap.get(key);
                    names.append(" | ").append(name);
                }
                record.setTagStr(names.substring(3));
            }
        }

        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanSellerDeviceParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanSellerDevice getOldEntity(NanSellerDeviceParam param) {
        return this.getById(getKey(param));
    }

    private NanSellerDevice getEntity(NanSellerDeviceParam param) {
        NanSellerDevice entity = new NanSellerDevice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
